package com.tal.live.moudels.weclassroom.controller;

import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.tal.live.annotation.ApiVersion;
import com.tal.live.annotation.ApiVersionConstant;
import com.tal.live.moudels.trc.helper.TrcHttpHelper;
import com.tal.live.pojo.dto.DocumentQuery;
import com.tal.live.pojo.dto.QueryTicket;
import com.tal.live.pojo.model.RestResult;
import com.tal.live.pojo.model.RestResultBuilder;
import com.tal.live.utils.validator.ValidatorUtils;
import com.tal.live.utils.validator.group.QueryGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * <Description>
 *
 * @author xianliru@100tal.com
 * @version 1.0
 * @createDate 2019/10/14 16:19
 */

@Slf4j
@Api( value = "trc",description="教研请求接口")
@RequestMapping("trc")
@RestController
public class TrcController {

    @Autowired
    private TrcHttpHelper trcHttpHelper;


    @PostMapping("query/ticket")
    @ApiOperation(value = "获取ticket", notes = "获取ticket")
    @ApiVersion(group = {ApiVersionConstant.QING_LUAN_V1})
    public RestResult queryTicket(@RequestBody QueryTicket ticket){
        ValidatorUtils.validateEntity(ticket, QueryGroup.class);

        String result = trcHttpHelper.queryTicket(ticket);
        return responeBuild(result);
    }

    @ApiOperation(value = "获取个人文档列表", notes = "获取个人文档列表")
    @ApiVersion(group = {ApiVersionConstant.QING_LUAN_V1})
    @PostMapping("query/doc/page")
    public RestResult queryDocumentByPage(@RequestBody DocumentQuery documentQuery){
        ValidatorUtils.validateEntity(documentQuery, QueryGroup.class);

        String result = trcHttpHelper.queryDocumentByPage(documentQuery);

        return responeBuild(result);
    }


    @ApiOperation(value="课件id查询编辑地址", notes="课件id查询编辑地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "id", value = "课件ID", example = "ss", required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query", name = "ticket", value = "ticket值", example= "ss", required = true, dataType = "String"),
    })
    @ApiVersion(group = {ApiVersionConstant.QING_LUAN_V1})
    @RequestMapping(value = "/course/edit/url",method = RequestMethod.GET)
    public RestResult getCourseWareEditUrl(@RequestParam("id") String id,@RequestParam("ticket") String ticket){

        String result = trcHttpHelper.getCourseWareEditUrl(id,ticket);

        return responeBuild(result);
    }

    @ApiOperation(value="个人云盘访问地址", notes="个人云盘访问地址")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType="query", name = "ticket", value = "ticket值", example = "ss",required = true, dataType = "String")
    })
    @ApiVersion(group = {ApiVersionConstant.QING_LUAN_V1})
    @RequestMapping(value = "/query/storage/index/url",method = RequestMethod.GET)
    public RestResult getQueryStorageIndexUrl(@RequestParam("ticket") String ticket){
        String result = trcHttpHelper.getQueryStorageIndexUrl(ticket);
        System.out.println(result);
        return responeBuild(result);
    }



    private RestResult responeBuild(String result){
        JsonElement je = new JsonParser().parse(result);
        boolean asBoolean = je.getAsJsonObject().get("success").getAsBoolean();
        if(asBoolean){
            return RestResultBuilder.builder().success(je.getAsJsonObject().get("data").getAsString()).build();
        }else {
            return RestResultBuilder.builder().failure(je.getAsJsonObject().get("message").getAsString()).build();
        }
    }


}
